import { logMgr } from "./LogMgr";

class StorageMgr{
    public static instance: StorageMgr = null
    public static getInstance() {
        if (!StorageMgr.instance) {
            StorageMgr.instance = new StorageMgr()
        }
        return StorageMgr.instance
    }

    private _bInit:boolean = false

    private _loginCache:Map<string,string> = new Map<string,string>()

    private _setItemCache:(key: string, value: string) => void = null
    private _getItemCache:(key: string) => string | null = null
    private _removeItem:(key: string) => void = null

    init(){
        if(this._bInit) return
        this._bInit = true

        this._setItemCache = localStorage.setItem
        this._getItemCache = localStorage.getItem
        this._removeItem = localStorage.removeItem

        // if(!GameConfig.releaseVersion){
        //     localStorage.setItem = ()=>{
        //         logMgr.warn(`请使用StorageMgr.setItem`)
        //     }
        //     localStorage.getItem = (key: string)=>{
        //         logMgr.warn(`请使用StorageMgr.getItem`)
        //         return null
        //     }
        //     localStorage.removeItem = (key: string)=>{
        //         logMgr.warn(`请使用StorageMgr.removeItem`)
        //     }
        // }
    }

    // setUserId(id:string){
    //     this._userId = id
    // }

    /**
     * 用于处理本次登录标识
     * @param key 
     * @param value 
     */
    setLoginFlag(key:string,value:string){
        this._loginCache.set(key,value)
    }

    getLoginFlag(key:string):string{
        return this._loginCache.get(key)
    }

    /**
     * 进入登录界面调用
     */
    clearLoginFlag(){
        this._loginCache.clear()
    }

    setDayFlag(key:string,value:string){

    }


    setItem(key:string, value:string) {
        this._setItemCache.call(localStorage,key, value)
    }

    getItem(key:string):string {
        return this._getItemCache.call(localStorage,key)
    }

    clear() {
        localStorage.clear();
    }
    
    removeItem(key:string) {
        this._removeItem.call(localStorage,key)
    }
}

export let storageMgr = StorageMgr.getInstance()
